package com.atyuntuku.yuntuku.manager;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.atyuntuku.yuntuku.config.CosClientConfig;
import com.atyuntuku.yuntuku.dto.file.UploadPictureResult;
import com.atyuntuku.yuntuku.exception.ErrorCode;
import com.atyuntuku.yuntuku.exception.ThrowUtils;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectResult;
import com.qcloud.cos.model.ciModel.persistence.ImageInfo;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Array;
import java.util.Date;
import java.util.List;

/**
 * ClassName: FileManager
 * Package: com.atyuntuku.yuntuku.manager
 * Description:
 *
 * @Author qinglong wei
 * @Create 2025/10/10 11:35
 * @Version 1.0
 */
@Service
public class FileManager {
    @Resource
    CosClientConfig cosClientConfig;
    @Resource
    COSManager cosManager;

   public void validatePicture(MultipartFile multipartFile)
   {
       //校验文件合法性
       ThrowUtils.throwIf(multipartFile == null, ErrorCode.PARAMS_ERROR,"图片不能为空");

       final long One_M = 1024*1024;
       ThrowUtils.throwIf(multipartFile.getSize() > 6 * One_M,ErrorCode.PARAMS_ERROR,"图片大小不能超过6M");
       //检查文件是不是规定内的图片文件
       final List<String> Allow_Format_List = CollectionUtil.toList("jpeg","jpg","png","webp");
       String suffix = FileUtil.getSuffix(multipartFile.getOriginalFilename());
       ThrowUtils.throwIf(!Allow_Format_List.contains(suffix),ErrorCode.PARAMS_ERROR,"图片类型错误");
   }

   public UploadPictureResult uploadpicture(MultipartFile multipartFile,String uploadPathPrefix)
   {
       validatePicture(multipartFile);
       String oringalfilename = multipartFile.getOriginalFilename();
       //生成图片上传地址
       String uuid = RandomUtil.randomString(16);
       String filename = String.format("%s_%s.%s", DateUtil.formatDate(new Date()),uuid,FileUtil.getSuffix(oringalfilename));
      String uploadpath = String.format("%s/%s",uploadPathPrefix,filename);
       File file = null;
       UploadPictureResult uploadPictureResult = null;
       try {
           file =File.createTempFile(uploadpath,null);
           multipartFile.transferTo(file);//转到本地临时存储
           PutObjectResult putObjectResult = cosManager.putObjectResult(uploadpath,file);
           //获取图片信息对象
           ImageInfo imageInfo = putObjectResult.getCiUploadResult().getOriginalInfo().getImageInfo();

           //计算宽高
           int picWidth = imageInfo.getWidth();
           int picHeight = imageInfo.getHeight();
           double picScale = NumberUtil.round(picWidth*1.0/picHeight,2).doubleValue();
           uploadPictureResult = new UploadPictureResult();
           uploadPictureResult.setUrl(cosClientConfig.getHost()+"/"+uploadpath);
           uploadPictureResult.setPicName(FileUtil.mainName(oringalfilename));
           uploadPictureResult.setPicSize(FileUtil.size(file));
           uploadPictureResult.setPicWidth(picWidth);
           uploadPictureResult.setPicHeight(picHeight);
           uploadPictureResult.setPicScale(picScale);
           uploadPictureResult.setPicFormat(imageInfo.getFormat());
       } catch (IOException e) {
           e.printStackTrace();
       }
       finally {
           if (file.exists())
           {
               boolean hasdeleted = file.delete();
               ThrowUtils.throwIf(!hasdeleted,ErrorCode.SYSTEM_ERROR,"上传失败，系统错误");
           }
       }

       return uploadPictureResult;
       
   }
}
